Imports System.Data

Partial Class WebForms_Referral
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = ParentPage

        If IsNothing(ViewState("CID")) Then
            sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
        Else
            sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
        End If

        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Select Case PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Dim dao As New DAOMain.POLICY_REF_DTL_DAO
            Dim vo As New DAOMain.POLICY_REF_DTL_VO
            Dim lfunc As New PolyFunc.ASPFunc
            Dim lookup As New LookUp.FillLookUp

            Call InitVars()

            policy_td.InnerText = ViewState("PolicyNo")
            provider_td.InnerText = ViewState("Provider")
            name_td.InnerText = ViewState("CName")
            Call SetPageSecurity()

            Dim dt As DataTable = dao.ExecQuery("Select * From VW_ADVISOR_MSTR Order by 2")

            lfunc.BindDropDown(Advisor_ddl, dt, "advisor_name", "user_name")
            lookup.FillDropDown(Status_ddl, lookup.eLookUpType.other, "ISSUE_STATUS")

            Call LoadReferral()

            lookup = Nothing
            dao = Nothing
            vo = Nothing
        End If
    End Sub

    Private Sub LoadReferral()
        If IsNothing(ViewState("CID")) Then Exit Sub

        Dim dao As New DAOMain.POLICY_REF_DTL_DAO
        Dim vo As New DAOMain.POLICY_REF_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim LookUp As New LookUp.FillLookUp

        vo.REFERRAL_CD = ViewState("CID")
        vo.POLICY_SEQ_NO = ViewState("Seq_CD")

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                LastName_txt.Text = .Item("REF_LAST_NAME")
                FirstName_txt.Text = .Item("REF_FIRST_NAME")
                MiddleName_txt.Text = .Item("REF_MIDDLE_NAME")
                Remarks_txt.Text = .Item("REMARKS")
                Status_ddl.SelectedValue = .Item("STATUS_CD")
                Advisor_ddl.SelectedValue = .Item("USER_NAME")
            End With
        End If

        LookUp = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim dao As New DAOMain.POLICY_REF_DTL_DAO
        Dim vo As New DAOMain.POLICY_REF_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc

        With vo
            .POLICY_SEQ_NO = ViewState("Seq_CD")
            .REF_LAST_NAME = LastName_txt.Text
            .REF_FIRST_NAME = FirstName_txt.Text
            .REF_MIDDLE_NAME = MiddleName_txt.Text
            .STATUS_CD = Status_ddl.SelectedValue.ToString
            .USER_NAME = Advisor_ddl.SelectedValue.ToString
            .REMARKS = Remarks_txt.Text

            Select Case IsNothing(ViewState("CID"))
                Case True
                    .REFERRAL_CD = cFunc.GetLastID("policy_issue_cd", "policy_issue_dtl")
                    .REF_CREA_BY = Session("UserName")
                    .REF_CREA_DT = Now

                    If dao.Create_Record(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        PolyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
                Case False
                    .REFERRAL_CD = ViewState("CID")
                    .REF_LAST_UPD_BY = Session("UserName")
                    .REF_LAST_UPD_DT = Now

                    If dao.Update(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        PolyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
            End Select
        End With

        polyFunc = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        If Not IsNothing(ViewState("PageSource")) Then
            InitSessions()
            Response.Redirect("~/" & strPath & "/Search_Referrals.aspx")
        Else
            InitSessions()
            Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
        End If
    End Sub

    
End Class
